<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyTablet as CompanyTabletModel;
use app\common\model\Company as CompanyModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;

/**
 * 寺院牌位控制器
 * Class Company
 * @package app\home\controller
 */
class Companytablet extends Base
{
    protected $moren = [
        'name' => '牌位',
        'days' => '1,3,5,7,30,90,180,360',
        'standard' =>array(
            [
                'name' => '消灾延寿禄位',
                'price' => 0,
                'beijing' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%89%8C%E4%BD%8D/%E9%95%BF%E7%94%9F.png',
                'top' => '佛光注照',
                'bottom' => '长生禄位',
                'left' => '福慧增长 吉祥如意',
                'right' => '消灾延寿 身心健康'
            ],
            [
                'name' => '往生莲位',
                'price' => 3,
                'beijing' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%89%8C%E4%BD%8D/%E5%BE%80%E7%94%9F.png',
                'top' => '佛光接引',
                'bottom' => '往生莲位',
                'left' => '离地狱出火坑 愿成佛度众生',
                'right' => '闹钟生烦恼轻 智慧长菩提生'
            ]
        )
    ];
    /**
     * [getprice  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function getprice()
    {
        $param = input('param.');
        $model = new CompanyTabletModel;
        $cmodel = new CompanyModel;
        try{
            $data = $model->findData($param['id'],'comid');
            if(!empty($data)){
                $data['standard'] = unserialize($data['standard']);
            }else{
                $data = $this->moren;
            }
            $company = $cmodel->findData($param['id'],'id','mch_id,much_secret,wx_appid,wx_secret,is_son');
            if($company['is_son'] == 1){
                if(empty($company['mch_id']) || empty($company['wx_appid'])){
                    return admin_json(2, '暂未开通');
                }
            }else{
                if(empty($company['wx_appid']) || empty($company['wx_secret']) || empty($company['mch_id']) || empty($company['much_secret'])){
                    return admin_json(2, '暂未开通');
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [index  获取牌位]
     * @param
     * @author [C。]
     * @return json
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyTabletModel;
        try{
            $data = $model->findData($param['id'],'comid','standard,name,days');
            if(empty($data)){
                $data = $this->moren;
            }else{
                $data['standard'] = unserialize($data['standard']);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [gettablet  获取牌位墙]
     * @param
     * @author [C。]
     * @return json
     */
    public function gettablet()
    {
        $param = input('param.');
        $model = new CompanyTabletModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','standard,name,days');
            if(empty($data)){
                $data = $this->moren;
            }else{
                $data['standard'] = unserialize($data['standard']);
            }
//            echo '<pre>';
//            print_r($data['standard']);die;
//            return admin_json(1, '获取成功',$data);
            $order = $cmodel->findData($param['order_id'],'id','id,gongdeng,day,name,to_name,content,paytime,bless,tablet_type,create_time');
            foreach ($data['standard'] as &$v){
                if($v['name'] == $order['gongdeng']){
                    $order['standard'] = $v;
                }
            }
            $order['residue'] = $order['paytime'] + $order['day']*24*3600 - time();
            $order['residue'] = time2string($order['residue']);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$order);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyTabletModel;
        $param = input('param.');
        try{
            $gongdeng = $model->findData($param['comid'],'comid','comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$param['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}